//
//  HMHomeTableViewCell.swift
//  WeiBo
//
//  Created by Apple on 16/9/21.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

/*
    1. 原创微博视图
    2. 转发微博视图
    3. toolBar视图
 
 */

//  子控件之间的间距
let HMHomeTableViewCellMargin: CGFloat = 10

//  自定义首页cell
class HMHomeTableViewCell: UITableViewCell {
    //  toolbar的顶部约束
    var toolBarTopConstraint: Constraint?
    
    var statusViewModel: HMStatusViewModel? {
        didSet {
            print("赋值")
            originalView.statusViewModel = statusViewModel
            //  卸载上次约束
            toolBarTopConstraint?.uninstall()
            //  转发微博，更新约束，绑定数据, 显示转发微博视图
            if statusViewModel?.status?.retweeted_status != nil {
                //
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                retweetView.statusViewModel = statusViewModel
                retweetView.isHidden = false
                
            } else {
                //  没有转发微博，隐藏转发微博，更新约束
                retweetView.isHidden = true
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            //  设置toolbar数据
            toolBar.statusViewModel = statusViewModel
            
            
        }
    }
    
    //  原创微博视图
    private lazy var originalView: HMStatusOriginalView = {
        let view = HMStatusOriginalView()
        
        return view
    }()
    //  toolBar视图
    private lazy var toolBar: HMStatusToolBar = {
        let toolBar = HMStatusToolBar()
        return toolBar
    }()
    //  转发视图
    private lazy var retweetView: HMStatusRetweetView = {
        let view = HMStatusRetweetView()
        
        return view
    }()
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
    
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        //  设置约束
        originalView.snp_makeConstraints { (make) in
            make.left.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
            make.trailing.equalTo(contentView)
            
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
        }
        
        toolBar.snp_makeConstraints { (make) in
            make.leading.equalTo(retweetView)
            make.trailing.equalTo(retweetView)
            toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
            //  关键约束
            make.bottom.equalTo(contentView)
            
        }
        
        
        
        
        
        
        
//        contentView.snp_makeConstraints { (make) in
//            make.bottom.equalTo(toolBar)
//            make.leading.equalTo(self)
//            make.trailing.equalTo(self)
//            make.top.equalTo(self)
//        }
        
    }
    
    
    
}
